package com.ossjk.oa.finance.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.finance.entity.Reimburse;
import com.ossjk.oa.finance.service.IReimburseService;
import com.ossjk.oa.finance.service.ISubreimburseService;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.service.IDepartmentService;
import com.ossjk.oa.system.service.IEmployeeService;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author chair
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/finance/reimburse")
public class ReimburseController extends BaseController {
	@Autowired
	private IReimburseService iReimburseService;
	@Autowired
	private ISubreimburseService iSubreimburseService;
	@Autowired
	private IEmployeeService iEmployeeService;
	@Autowired
	private IDepartmentService iDepartmentService;

	@PostMapping("/pay")
	public Object pay(String eid, Integer count, String mth, String descb, String cpttype) {

		Employee emp = iEmployeeService.selectById(eid);

		if (iReimburseService.pay(emp, count, mth, descb, cpttype)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("00100000")
	@GetMapping("/list")
	public Object list(String eid, String did, Integer rwst, String mth, Page page, HttpServletRequest req) {

		if (mth == null) {
			// 先去session中看有无旧值
			if (req.getSession().getAttribute("mth4Rm") != null) {
				mth = (String) req.getSession().getAttribute("mth4Rm");
			} else {
				Calendar now = Calendar.getInstance();
				if (now.get(Calendar.DAY_OF_MONTH) > 16) {
					mth = CommonMethod.calMth(0, now.getTime()); // 默认本月
				} else {
					mth = CommonMethod.calMth(-1, now.getTime()); // 默认上月
				}
				req.getSession().setAttribute("mth4Rm", mth);
			}
		} else {
			req.getSession().setAttribute("mth4Rm", mth);
		}

		// 列表内容
		Wrapper wrapper = Condition.create().isWhere(true).orderBy("edtm", true);
		if (!CommonUtil.isBlank(eid)) {
			wrapper.eq("eid", eid);
		}
		if (!CommonUtil.isBlank(did)) {
			Department dpt = iDepartmentService.selectById(did);
			if (dpt != null && dpt.getLevel().intValue() > 1) {
				wrapper.eq("did", did);
			}
		}

		if (!CommonUtil.isBlank(rwst)) {
			/*
			 * 审核状态：1-待审核、2-通过、3-不通过，4-公司待审核、5-公司不通过，6-公司通过、 <option value="1">待审核</option>
			 * <option value="6">公司不通过</option> <option value="5">公司审核通过</option><option
			 * value="7">已发放</option>
			 */
			if (rwst < 5) {
				wrapper.le("rwst", 4);
			} else {
				wrapper.eq("rwst", rwst);
			}
		}
		// System.out.println("rwst = " + rwst);

		wrapper.eq("left(edtm,7)", mth);

		Map datas = new HashMap();
		datas.put("navi", iReimburseService.selectPreOrNextByMth(mth));
		datas.put("datas", iReimburseService.selectVoPage(page, wrapper));

		return responseSuccess(datas);
	}

	@RequiresPermissions("00100050")
	@GetMapping("/listM")
	public Object listMth(String eid, String did, String mth) {

		if (mth == null) {
			Calendar now = Calendar.getInstance();
			mth = CommonMethod.calMth(-1, now.getTime()); // 默认上月
		}

		// 列表内容
		Wrapper wrapper = Condition.create().isWhere(true);
		if (!CommonUtil.isBlank(eid)) {
			wrapper.eq("eid", eid);
		}
		if (!CommonUtil.isBlank(did)) {
			Department dpt = iDepartmentService.selectById(did);
			if (dpt != null && dpt.getLevel().intValue() > 1) {
				wrapper.eq("did", did);
			}
		}
		// 公司审批通过
		wrapper.ge("rwst", 6).eq("left(edtm,7)", mth).groupBy("eid,left(edtm,7),dname,rname").orderBy("dname,rname", true);

		Map datas = new HashMap();
		datas.put("navi", iReimburseService.selectPreOrNextByMth(mth));
		datas.put("datas", iReimburseService.selectMthReimVos(wrapper));

		return responseSuccess(datas);
	}

	@RequiresPermissions("001000000005")
	@PostMapping("/insert")
	public Object insert(Reimburse reimburse, String[] sname, String[] samount, String[] sisreceipt,
			String[] sremarks) {

		reimburse.setRwst(new BigDecimal(1));
		if (reimburse.getEdtm() == null) {
			reimburse.setEdtm(reimburse.getSttm());
		}
		if (iReimburseService.insert(reimburse, sname, samount, sisreceipt, sremarks)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("001000000015")
	@PutMapping("/updateDetail")
	public Object updateDetail(Reimburse reimburse, String[] sname, String[] samount, String[] sisreceipt,
			String[] sremarks) {
		Wrapper wrapper2 = Condition.create().eq("isdelete", "1");
		if (reimburse.getRwst() != new BigDecimal(1)) {
			reimburse.setRwtm(new Date());
			reimburse.setRwid(getCurrnetUserId());
		}
		if (iReimburseService.updateDetail(reimburse, sname, samount, sisreceipt, sremarks)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("001000000015")
	@GetMapping("/toUpdateDetail/{id}")
	public Object toUpdateDetail(@PathVariable("id") String id) {
		Map map = new HashMap();
		Reimburse reimburse = iReimburseService.selectById(id);
		map.put("reimburse", iReimburseService.selectById(id));
		Wrapper wrapper = Condition.create().like("rid", id);
		map.put("subreimburse", iSubreimburseService.selectList(wrapper));
		Employee employee = iEmployeeService.selectById(reimburse.getRwid());
		map.put("employee", iEmployeeService.selectById(reimburse.getRwid()));
		return responseSuccess(map);
	}

	@RequiresPermissions("00100000")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		return responseSuccess(iReimburseService.selectById(id));
	}

	@RequiresPermissions("00100000")
	@PutMapping("/update")
	public Object update(Reimburse reimburse) {
		if (iReimburseService.updateById(reimburse)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@RequiresPermissions("001000000025")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iReimburseService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

}
